package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.movecar.R;
import com.movecar.model.SiteEventBean;
import com.movecar.model.ThemeMatchDetailBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.RequestResultCallback;
import com.movecar.utils.imageloader.Constants;
import com.movecar.widget.CustomProgressDialog;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 主页沾光活动
 * 
 * @author Zhao.JQ
 *
 */
public class GroupSiteEventAlertActivity extends Activity implements OnClickListener {
	private TextView title_text;
	private ImageView group_img;
	private TextView content_text;
	private String tdcId;
	private String code;
	private CustomProgressDialog progressDialog = null;
	private ImageLoader imageLoader = null;
	private SiteEventBean mSiteEventBean;
	private Button ok;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.site_event_alert);
		tdcId = getIntent().getStringExtra("tdcId");
		code = getIntent().getStringExtra("code");
		initView();
	}

	private void initView() {
		imageLoader = ImageLoader.getInstance();
		title_text = (TextView) findViewById(R.id.title_text);
		content_text = (TextView) findViewById(R.id.content_text);
		group_img = (ImageView) findViewById(R.id.group_img);

		ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(this);
		startProgressDialog();
		getDetail();
	}

	@SuppressWarnings("unchecked")
	private void getDetail() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); //
		// 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "queryOnSiteActivityNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("tdcId", tdcId));
		params.add(new BasicNameValuePair("code", code));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {
				stopProgressDialog();
				String result = (String) object;

				Gson gson = new Gson();
				try {
					mSiteEventBean = gson.fromJson(result, SiteEventBean.class);
				} catch (Exception e) {
					return;
				}
				if (null != mSiteEventBean) {
					stopProgressDialog();
					if ("0".equals(mSiteEventBean.getResultCode())) {

						title_text.setText(mSiteEventBean.getTitle());
						content_text.setText(mSiteEventBean.getContent());
						imageLoader.displayImage(AppConfig.HOST + mSiteEventBean.getLogo(), group_img,
								Constants.image_display_options, AppConfig.animateListener);
						ok.setText("分享体验，获得奖励");

					} else {
						Alert.error(GroupSiteEventAlertActivity.this, mSiteEventBean.getResultCode());
					}

				} else {
					stopProgressDialog();
					Alert.error(GroupSiteEventAlertActivity.this, "系统异常");
				}

			}

			@Override
			public void onFail(String exception) {

				stopProgressDialog();
				Alert.error(GroupSiteEventAlertActivity.this, "与服务器连接失败，请检查网络");

			}
		}, ThemeMatchDetailBean.class, AppConfig.GROUP).execute(params);
	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.ok:

			if ("0".equals(mSiteEventBean.getSectionType())) {

				Intent next = new Intent(this, AddMyRedEnvelopesShareActivity.class);
				next.putExtra("shareFlag", 0);
				next.putExtra("TdcId", mSiteEventBean.getTdcId());
				next.putExtra("sectionId", mSiteEventBean.getSectionId());
				startActivity(next);
			} else if ("2".equals(mSiteEventBean.getSectionType())) {
				Intent next = new Intent(this, AddMyShareActivity.class);
				next.putExtra("shareFlag", 1);
				next.putExtra("TdcId", mSiteEventBean.getTdcId());
				next.putExtra("sectionId", mSiteEventBean.getSectionId());
				next.putExtra("sectionName", mSiteEventBean.getSectionName());
				startActivity(next);
			}

			setResult(RESULT_OK);
			finish();

			break;
		default:
			break;
		}

	}

}
